//
//  MKUILayoutConstraint.h
//  MKUI
//	进行简单的视图布局约束的基类
//  Created by sushangjin on 15/4/14.
//  Copyright (c) 2015年 sushangjin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "MKDefine_EnumUtil.h"
#import "MKCGRect.h"
#import "CGGeometry+MKUI.h"
#import "MKUILayoutConstraintItemAttribute.h"
NS_ASSUME_NONNULL_BEGIN
//被布局的元素要支持的方法
@protocol MKUILayoutConstraintItemProtocol <MKUILayoutConstraintItemAttributeProtocol>

- (CGSize)sizeOfLayout;//返回尺寸信息
- (BOOL)hidden;//是否隐藏,默认为NO
- (void)setLayoutTransform:(CGAffineTransform)transform;//以布局bounds为中心点,对布局作为一个整体,设置底下元素的仿射矩阵
@optional
- (CGSize)sizeThatFits:(CGSize)size;
- (CGSize)sizeThatFits:(CGSize)size resizeItems:(BOOL)resizeItems;//适合于容器
- (void)layoutItemsWithResizeItems:(BOOL)resizeItems;//适合于容器
@end

typedef NS_ENUM(NSInteger, MKUILayoutConstraintVerticalAlignment) {
	MKUILayoutConstraintVerticalAlignmentCenter  = 0,
	MKUILayoutConstraintVerticalAlignmentTop     = 1,
	MKUILayoutConstraintVerticalAlignmentBottom  = 2,
};
MKAS_EnumTypeCategories(MKUILayoutConstraintVerticalAlignment)
CG_INLINE MKCGRectAlignment MKCGRectAlignmentFromMKUILayoutConstraintVerticalAlignment(MKUILayoutConstraintVerticalAlignment align){
	return align==MKUILayoutConstraintVerticalAlignmentCenter?MKCGRectAlignmentMid:(align==MKUILayoutConstraintVerticalAlignmentTop?MKCGRectAlignmentMin:MKCGRectAlignmentMax);
}

typedef NS_ENUM(NSInteger, MKUILayoutConstraintHorizontalAlignment) {
	MKUILayoutConstraintHorizontalAlignmentCenter = 0,
	MKUILayoutConstraintHorizontalAlignmentLeft   = 1,
	MKUILayoutConstraintHorizontalAlignmentRight  = 2,
};
MKAS_EnumTypeCategories(MKUILayoutConstraintHorizontalAlignment)
CG_INLINE MKCGRectAlignment MKCGRectAlignmentFromMKUILayoutConstraintHorizontalAlignment(MKUILayoutConstraintHorizontalAlignment align){
	return align==MKUILayoutConstraintHorizontalAlignmentCenter?MKCGRectAlignmentMid:(align==MKUILayoutConstraintHorizontalAlignmentLeft?MKCGRectAlignmentMin:MKCGRectAlignmentMax);
}

typedef NS_ENUM(NSUInteger, MKUILayoutConstraintDirection) {
	MKUILayoutConstraintDirectionVertical,//垂直布局
	MKUILayoutConstraintDirectionHorizontal,//水平布局
};
MKAS_EnumTypeCategories(MKUILayoutConstraintDirection)

@interface MKUILayoutConstraint : NSObject<MKUILayoutConstraintItemProtocol,NSCopying>{
	@protected
	NSMutableArray<id<MKUILayoutConstraintItemProtocol>> *_items;
}
@property(nonatomic,assign) BOOL hidden;//是否隐藏,默认为NO
@property(nonatomic,strong,nullable) NSArray<id<MKUILayoutConstraintItemProtocol>> *items;//@[id<MKUILayoutConstraintItemProtocol>]
@property(nonatomic,assign) CGRect bounds;//在bounds的区域内布局
@property(nonatomic,assign) BOOL layoutHiddenItem;//是否布局隐藏元素,默认为NO
- (instancetype)initWithItems:(nullable NSArray<id<MKUILayoutConstraintItemProtocol>> *)items bounds:(CGRect)bounds;
- (void)addItem:(id<MKUILayoutConstraintItemProtocol>)item;
- (void)removeItem:(id<MKUILayoutConstraintItemProtocol>)item;
- (void)replaceItem:(id<MKUILayoutConstraintItemProtocol>)oldItem with:(nullable id<MKUILayoutConstraintItemProtocol>)newItem;//进行item的替换,如果items中元素为MKUILayoutConstraintItemWrapper话,会检查期origin是否==oldItem
- (void)layoutItems;//进行布局,子类实现
@property(nonatomic,readonly,nullable) NSArray<id<MKUILayoutConstraintItemProtocol>> *layoutedItems;//显示的元素,@[id<MKUILayoutConstraintItemProtocol>]
@property(nonatomic,readonly,nullable) NSArray<id<MKUILayoutConstraintItemProtocol>> *visiableItems;//显示的元素,@[id<MKUILayoutConstraintItemProtocol>]
/**
 *  计算适应的bounds的尺寸
 *	@override	子类重量写
 *  @param size 限定的尺寸
 *
 *  @return 合适的尺寸
 */
- (CGSize)sizeThatFits:(CGSize)size;

/**
 *	将布局中的元素,作为一个整体,以self.bounds中心点为坐标原点,应用上transform仿射变换矩阵
 *	例如:transform=CGAffineTransformMakeRotation(M_PI_2),则会以对每一个item,以bounds为中心点,旋转90度
 *	@param transform 变换矩阵
 */
- (void)setLayoutTransform:(CGAffineTransform)transform;

/**
 *  将布局中的元素,作为一个整体,以self.bounds中心点为坐标原点,应用上transform仿射变换矩阵.该方法计算出transform添加上中心点变换的矩阵运算
 *	@param transform 变换矩阵
 *  @return 以bounds为中心点的变换矩阵
 */
- (CGAffineTransform)convertTransfromWith:(CGAffineTransform)transform toItem:(id<MKUILayoutConstraintItemProtocol>)item;
@end

@interface UIView(MKUILayoutConstraintItemProtocol)<MKUILayoutConstraintItemProtocol>
@end

@interface UICollectionViewLayoutAttributes(MKUILayoutConstraintItemProtocol)<MKUILayoutConstraintItemProtocol>
@end

NS_ASSUME_NONNULL_END
